# Maintainer: Christian Hesse <mail@eworm.de>
# Contributor: Gustavo Alvarez <sl1pkn07@gmail.com>

pkgbase=lpeg
pkgname=(lua-lpeg lua51-lpeg lua52-lpeg lua53-lpeg)
pkgver=1.0.2
pkgrel=4
arch=('x86_64')
url='http://www.inf.puc-rio.br/~roberto/lpeg'
license=('MIT')
makedepends=('lua' 'lua51' 'lua52' 'lua53')
source=("http://www.inf.puc-rio.br/~roberto/${pkgbase}/${pkgbase}-${pkgver}.tar.gz"
        'LICENSE')
sha256sums=('48d66576051b6c78388faad09b70493093264588fcd0f258ddaab1cdd4a15ffe'
            '6be7688c9c648e7c7cde693081be938c869a10aa4de09fe0a4c4c1d66b3cf03d')

prepare() {
  sed 's/^CFLAGS =/CFLAGS +=/' -i "${pkgbase}-${pkgver}"/makefile
  sed 's/^COPT =/# COPT =/' -i "${pkgbase}-${pkgver}"/makefile

  cp -r "${pkgbase}-${pkgver}" "${pkgbase}-${pkgver}"-51
  cp -r "${pkgbase}-${pkgver}" "${pkgbase}-${pkgver}"-52
  cp -r "${pkgbase}-${pkgver}" "${pkgbase}-${pkgver}"-53
}

build() {
  cd "${pkgbase}-${pkgver}"
  make LUADIR=/usr/include

  cd "${srcdir}/${pkgbase}-${pkgver}-51"
  make LUADIR=/usr/include/lua5.1

  cd "${srcdir}/${pkgbase}-${pkgver}-52"
  make LUADIR=/usr/include/lua5.2
  
  cd "${srcdir}/${pkgbase}-${pkgver}-53"
  make LUADIR=/usr/include/lua5.3
}

check() {
  echo "Testing with Lua 5.4"
  cd "${srcdir}/${pkgbase}-${pkgver}"
  lua test.lua

  echo "Testing with Lua 5.1"
  cd "${srcdir}/${pkgbase}-${pkgver}-51"
  lua5.1 test.lua

  echo "Testing with Lua 5.2"
  cd "${srcdir}/${pkgbase}-${pkgver}-52"
  lua5.2 test.lua
  
  echo "Testing with Lua 5.3"
  cd "${srcdir}/${pkgbase}-${pkgver}-53"
  lua5.3 test.lua
}

package_lua-lpeg() {
  pkgdesc='Pattern-matching library for Lua 5.4'
  depends=('lua')

  cd "${srcdir}/${pkgbase}-${pkgver}"
  install -Dm0755 ${pkgbase}.so "${pkgdir}"/usr/lib/lua/5.4/${pkgbase}.so
  install -Dm0644 re.lua "${pkgdir}"/usr/share/lua/5.4/re.lua
  install -Dm0644 ../LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_lua51-lpeg() {
  pkgdesc='Pattern-matching library for Lua 5.1'
  depends=('lua51')

  cd "${srcdir}/${pkgbase}-${pkgver}-51"
  install -Dm0755 ${pkgbase}.so "${pkgdir}"/usr/lib/lua/5.1/${pkgbase}.so
  install -Dm0644 re.lua "${pkgdir}"/usr/share/lua/5.1/re.lua
  install -Dm0644 ../LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_lua52-lpeg() {
  pkgdesc='Pattern-matching library for Lua 5.2'
  depends=('lua52')

  cd "${srcdir}/${pkgbase}-${pkgver}-52"
  install -Dm0755 ${pkgbase}.so "${pkgdir}"/usr/lib/lua/5.2/${pkgbase}.so
  install -Dm0644 re.lua "${pkgdir}"/usr/share/lua/5.2/re.lua
  install -Dm0644 ../LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_lua53-lpeg() {
  pkgdesc='Pattern-matching library for Lua 5.3'
  depends=('lua53')

  cd "${srcdir}/${pkgbase}-${pkgver}-53"
  install -Dm0755 ${pkgbase}.so "${pkgdir}"/usr/lib/lua/5.3/${pkgbase}.so
  install -Dm0644 re.lua "${pkgdir}"/usr/share/lua/5.3/re.lua
  install -Dm0644 ../LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}
